
#include "connect/PipeConnectManager.h"
#include "connect/PipeConnect.h"

namespace KEngine
{
	IConnect* PipeConnectManager::create(const char* name)
	{
		PipeConnect* pipe = new PipeConnect(name,this);
		_connects[name] = pipe;
		return pipe;
	}

	void PipeConnectManager::disconnect(IConnect* p)
	{
		PipeConnect* pipe = static_cast<PipeConnect*>(p);
		PipeConnect* pipeRemote = static_cast<PipeConnect*>(pipe->getRemote());
		if(pipeRemote)
			pipeRemote->disconnect();

		pipe->disconnect();
	}

	void PipeConnectManager::releaseConnect(IConnect* p)
	{
		delete static_cast<PipeConnect*>(p);
	}

	bool PipeConnectManager::connect(const char* connect1,const char* connect2)
	{
		PipeConnect* pipe1 = static_cast<PipeConnect*>(get(connect1));
		PipeConnect* pipe2 = static_cast<PipeConnect*>(get(connect2));

		if(!pipe1 || !pipe2)
			return false;

		pipe1->connect(pipe2);
		pipe2->connect(pipe1);
		return true;
	}

	IConnect* PipeConnectManager::createPipe(const char* name,MsgQueue* queue)
	{
		PipeConnect* pipe = static_cast<PipeConnect*>(create(name));
		pipe->setMsgQueue(queue);

		return pipe;
	}
};
